var personas = {
		init:function(){
	$.getJSON("ubigeos/listaubigeos", function(listaubigeos){
		$.getJSON("tipopersonas/listatipos", function(listatipos) {
				setupGrid(listaubigeos, listatipos);
			});
	});

	var persona = this;	
			function setupGrid(listaubigeos,listatipos){
			$('#personas').jqGrid({
	           url:'personas/index',
	           datatype:'json',
	           mtype:'POST',
	           colNames:['id','Codigo','Nombres','Paterno','Materno','Fecha nacimiento','Direccion','Telefono','Movil',
	                     'E-mail','Dni','Estado civil','Sexo','Facultad','Escuela','Tipo','Nombre Completo','Ubigeo','foto'],
	           colModel:[
	                {name:"data[Persona][id]",hidden:true},
	                {name:"data[Persona][codigo]",index:'Persona.codigo',width:50,align:'left',
		            	   sortable:true,editoptions:{maxlength:50, size:25},editable:true,editrules:{required:true},
		            	   formoptions:{ rowpos:1, elmprefix:"(*)"},
		            	   searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}
		            	   },
	                {name:"data[Persona][nombre]",index:'Persona.nombre',width:80,align:'left',
	            	   sortable:true,editoptions:{maxlength:50, size:25},editable:true,editrules:{edithidden:true},
	            	   formoptions:{ rowpos:2, elmprefix:"(*)"},
	            	   searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}
	            	   },
						   	   
					{name:"data[Persona][ap_paterno]",index:'Persona.ap_paterno',width:80,align:'left',
	            	   sortable:true,editoptions:{maxlength:100, size:25},editable:true,editrules:{required:true},
	            	   formoptions:{ rowpos:3, elmprefix:"(*)"},
	            	   searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}
	            	   },
   					{name:"data[Persona][ap_materno]",index:'Persona.ap_materno',width:80,align:'left',
	            	   sortable:true,editoptions:{maxlength:100, size:25},editable:true,editrules:{required:true},
	            	   formoptions:{ rowpos:4, elmprefix:"(*)"},
	            	   searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}
	            	   },
   					{name:"data[Persona][fech_nacimiento]",index:'Persona.fech_nacimiento',width:60,editoptions:{size:15, maxlength:10},
	            	   editoptions:{size:12, 
	            	   dataInit:function(el){ $(el).datepicker({dateFormat:'yy-mm-dd'}); },            	 
	            	   defaultValue: function(){ 
	            		   var currentTime = new Date(); 
	            		   var month = parseInt(currentTime.getMonth() + 1); 
	            		   month = month <= 9 ? "0"+month : month; var day = currentTime.getDate(); 
	            		   day = day <= 9 ? "0"+day : day; var year = currentTime.getFullYear(); 
	            		   return year+"-"+month + "-"+day; } },	            		   
	            	   formoptions:{ rowpos:5, elmprefix:"(*)",elmsuffix:" yyyy-mm-dd" },	            	   
	            	   editable:true,sorttype:"date",editrules:{edithidden:true,date:true,required:false}},
            	   
   					{name:"data[Persona][direccion]",index:'Persona.direccion',hidden:true,width:100,align:'left',
	            	   sortable:true,editoptions:{maxlength:100, size:25},editable:true,editrules:{edithidden:true},
	            	   formoptions:{ rowpos:6, elmprefix:"(*)"},
	            	   searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}
	            	   },
   					{name:"data[Persona][telefono]",index:'Persona.telefono',hidden:true,width:50,align:'left',
	            	   sortable:true,editoptions:{maxlength:15, size:15},editable:true,editrules:{edithidden:true},
	            	   formoptions:{ rowpos:7, elmprefix:"(*)"},
	            	   searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}
	            	   },
	            	{name:"data[Persona][movil]",index:'Persona.movil',width:50,align:'left',
		            	   sortable:true,editoptions:{maxlength:15, size:15},editable:true,editrules:{required:true},
		            	   formoptions:{ rowpos:8, elmprefix:"(*)"},
		            	   searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}
		            	   },
		            {name:"data[Persona][mail]",index:'Persona.mail',width:50,align:'left',
		            	   sortable:true,editoptions:{maxlength:30, size:30},editable:true,editrules:{required:true},
		            	   formoptions:{ rowpos:9, elmprefix:"(*)"},
		            	   searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}
		            	   },
   					{name:"data[Persona][dni]",index:'Persona.dni',width:50,align:'left',
	            	   sortable:true,editoptions:{maxlength:8, size:10},editable:true,editrules:{required:true},
	            	   formoptions:{ rowpos:10, elmprefix:"(*)"},
	            	   searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}
	            	   },
   					{name:"data[Persona][estado_civil]",index:'Persona.estado_civil',hidden:true,width:50,align:'left',
	            	   sortable:true,editoptions:{maxlength:40, size:15},editable:true,edittype:"select",
	            	   editrules:{edithidden:true},editoptions:{value:"SOLTERO:SOLTERO;CASADO:CASADO;VIUDO:VIUDO"},	 
	            	   formoptions:{ rowpos:11, elmprefix:"(*)"},           	   
	            	   searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}
	            	   },
   					{name:"data[Persona][sexo]",index:'Persona.sexo',hidden:true,width:40,align:'left',
	            	   sortable:true,editoptions:{maxlength:15, size:20},editable:true,edittype:"select",editrules:{edithidden:true},
	            	   formoptions:{ rowpos:12, elmprefix:"(*)"},
	            	   editoptions:{value:"MASCULINO:MASCULINO;FEMENINO:FEMENINO}"},searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}
	            	   },
   					{name:"data[Persona][grupo]",index:'Persona.grupo',hidden:true,width:50,align:'left',
	            	   sortable:true,editoptions:{maxlength:40, size:15},editable:true,edittype:"select",
	            	   editrules:{edithidden:true},editoptions:{value:"-:-"},	 
	            	   formoptions:{ rowpos:13, elmprefix:"(*)"},           	   
	            	   searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}
	            	   },  
            	   {name:"data[Persona][grupo]",index:'Persona.subgrupo',hidden:true,width:50,align:'left',
	            	   sortable:true,editoptions:{maxlength:40, size:15},editable:true,edittype:"select",
	            	   editrules:{edithidden:true},editoptions:{value:"INGENIERIA DE SISTEMAS:INGENIERIA DE SISTEMAS;ENFERMERIA:ENFERMERIA;ADMINISTRACION:ADMINISTRACION;ECONOMIA:ECONOMIA;CONTABILIDAD:CONTABILIDAD;-:-"},	 
	            	   formoptions:{ rowpos:14, elmprefix:"(*)"},           	   
	            	   searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}
	            	   },  
		           
            	   {name:"data[Persona][tipopersona_id]",index:'Tipopersona.descripcion',hidden:true,width:45,align:'left',
   	            	sortable:true,editable:true,editoptions:{size:40, value:listatipos},editrules:{edithidden:true},
   	            	formoptions:{ rowpos:15, elmprefix:"(*)"},
   	            	edittype:"select",searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
	   	            	
		          {name:"data[Persona][nombre_completo]",index:'Persona.nombre_completo',hidden:true,width:100,align:'left',
	            	   sortable:true,editoptions:{maxlength:250, size:30},editable:true,//editrules:{edithidden:true},
	            	   formoptions:{ rowpos:16, elmprefix:""},	            	   
	            	   searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}
	            	   },		          
	            {name:"data[Persona][ubigeo_id]",index:'Ubigeo.distrito',hidden:true,width:45,align:'left',
	            	sortable:true,editable:true,editoptions:{size:40, value:listaubigeos},editrules:{edithidden:true},
	            	formoptions:{ rowpos:17, elmprefix:"(*)"},
	            	edittype:"select",searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
	            { name: 'Register_Id', index: 'Register_Id', width: 100, align: 'left', formatter: formatImage },

	            	   
	            /*			{name:"data[Demanda][demandatipo_id]",index:'Demanda.descripciontipo',width:150,align:'left',sortable:false,editable:true,edittype:"select", editrules:{required:true}, editoptions:{value:listademandatipos},searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}
	               ,formoptions:{ rowpos:2, elmprefix:"(*)"}},*/
	               
	           ],
		
			   pager:$('#personas-pager'),
	           rowNum:21,
	           rowList:[10,20,30],
	           mtype: "POST",
	           viewrecords:true,
	           caption:'Personas',
		        rownumbers: true, 
	           height:'500',
	           sortname: "Persona.id",
	           sortorder: "desc", 
	           editurl: 'personas/index'	                    
	       });
			$('#personas').navGrid("#personas-pager",{view:true,edit:true,add:true,del:true},
					{clearAfterEdit:false,reloadAfterSubmit:true, bottominfo:"Campos marcados con (*) son requeridos", closeOnEscape:true, afterSubmit:persona.afterSubmit},
					{clearAfterAdd:true,reloadAfterSubmit:true, bottominfo:"Campos marcados con (*) son requeridos", closeOnEscape:true,afterSubmit:persona.afterSubmit},
					{width:270,reloadAfterSubmit:true,closeOnEscape:true},
					{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew'],multipleSearch:true,closeOnEscape:true});	       
			$("#personas").jqGrid('filterToolbar',{stringResult: true,searchOnEnter : false});
	       jQuery("#personas").jqGrid('setGridWidth',$('#RightPane').width()-50);
			}}
		,
		afterSubmit: function(response){var respuesta = JSON.parse(response.responseText);return [respuesta.respuesta,respuesta.mensaje];}
};

function formatImage(cellValue, options, rowObject) {

    var imageHtml = "<img width='100' height='120' src='./app/webroot/img/fotos/" + rowObject[1] + ".jpg' />";
return imageHtml;
} 
